﻿using AnalyStock.DataManage;

namespace AnalyStock.WPFPage;

public partial class LoadDataPage : Page
{
    private DownloadDataType downDataType; //
    private string startDate, endDate, stockCode;

    public LoadDataPage()
    {
        InitializeComponent();
        ListBoxSele.ItemsSource = new[]
        {
            "⊙下载股票名录", //index 0
            "--------------",
            "⊙下载交易数据", //2
            "--------------",
            "⊙下载财务数据", //4
            "--------------",
            "⊙下载交易备份", //6
            "--------------"
        };
        btnLoadAllData.IsEnabled = false;
        btnLoadSingleData.IsEnabled = false;
        btnStop.IsEnabled = false;
        ListBoxSele.SelectionChanged += ListBoxSele_SelectionChanged;
        btnLoadAllData.Click += BtnLoadAllData_Click;
        btnLoadSingleData.Click += BtnLoadSingleData_Click;
        checkBoxByDayorPeriod.Click += CheckBoxByDayorPeriod_Click;
        btnStop.Click += BtnCancelProgress_Click;
    }

    private void ListBoxSele_SelectionChanged(object sender, EventArgs e)
    {
        switch (ListBoxSele.SelectedIndex)
        {
            case 0:
                btnLoadSingleData.IsEnabled = true;
                btnLoadAllData.IsEnabled = false;
                textBstartDate.Text = string.Empty;
                textBendDate.Text = string.Empty;
                break;
            case 2:
                btnLoadSingleData.IsEnabled = true;
                btnLoadAllData.IsEnabled = true;
                textBstartDate.Text = DateTime.Today.AddDays(-3).ToDateString();
                textBendDate.Text = DateTime.Today.ToDateString();
                checkBoxByDayorPeriod.IsEnabled = true;
                checkBoxByDayorPeriod.IsChecked = false;
                downDataType = checkBoxByDayorPeriod.IsChecked.Value
                    ? DownloadDataType.TradeOnPeriod
                    : DownloadDataType.TradeOnDaily;
                break;
            case 4:
                btnLoadSingleData.IsEnabled = true;
                btnLoadAllData.IsEnabled = true;
                textBstartDate.Text = $"{DateTime.Today.Year - 1}0101";
                textBendDate.Text = DateTime.Today.ToDateString();
                checkBoxByDayorPeriod.IsChecked = true;
                checkBoxByDayorPeriod.IsEnabled = false; //关闭单日全部股票数据导入功能
                downDataType = DownloadDataType.FinanceOnPeriod; //后台导入财务指标数
                break;
            case 6:
                btnLoadSingleData.IsEnabled = true;
                btnLoadAllData.IsEnabled = false;
                textBstartDate.Text = string.Empty;
                textBendDate.Text = DateTime.Today.AddDays(-1).ToDateString();
                break;
        }

        textPrograss1.Text = string.Empty;
        textPrograss2.Text = string.Empty;
    }

    private void CheckBoxByDayorPeriod_Click(object sender, RoutedEventArgs e)
    {
        downDataType = checkBoxByDayorPeriod.IsChecked.Value
            ? DownloadDataType.TradeOnPeriod
            : DownloadDataType.TradeOnDaily;
    }

    private async void BtnLoadAllData_Click(object sender, EventArgs e)
    {
        switch (ListBoxSele.SelectedIndex)
        {
            case 2 or 4:
                await DownloadByMultiTaskAsync();
                break;
            default:
                "请选择批量的下载选项...".NotifyMessage();
                break;
        }
    }

    private async void BtnLoadSingleData_Click(object sender, EventArgs e)
    {
        Cursor = Cursors.Wait;
        switch (ListBoxSele.SelectedIndex)
        {
            case 0:
                await LoadBasicNameOfStockAsync();
                break;
            case 2:
                await LoadInforDailyOfSingleStockAsync();
                break;
            case 4:
                await LoadFinacialIndexOfSingleStockAsync();
                break;
            case 6:
                await LoadBakDailyOfSingleDailyAsync();
                break;
        }

        Cursor = Cursors.Arrow;
    }

    /// <summary>
    ///     文本框输入格式核验
    /// </summary>
    /// <param name="downType"></param>
    /// <returns></returns>
    private bool IsWrongOfCheckFormatting(string downType)
    {
        startDate = textBstartDate.Text.Trim().ToLower();
        endDate = textBendDate.Text.Trim().ToLower();
        stockCode = textBCode.Text.Trim().ToUpper();
        switch (downType)
        {
            case "DownAllStockOnPeriod":
                if (startDate.Length != 8 || endDate.Length != 8)
                {
                    "日期格式有误(yyyyMMdd)，请核查...".ErrorMessage();
                    return true;
                }

                break;
            case "DownSingleStockOnPeriod":
                if (startDate.Length != 8 || endDate.Length != 8 || stockCode.Length < 9)
                {
                    "日期格式(yyyyMMdd)或股票代码有误，请核查...".ErrorMessage();
                    return true;
                }

                break;
            case "DownAllStockOnSingleDaily":
                if (endDate.Length != 8)
                {
                    "日期格式有误(yyyyMMdd)...".ErrorMessage();
                    return true;
                }

                break;
        }

        return false;
    }

    private async Task LoadInforDailyOfSingleStockAsync()
    {
        if (IsWrongOfCheckFormatting("DownSingleStockOnPeriod"))
        {
            return;
        }

        if ("追加交易信息前，要删除原有时间段交易信息，是否同意?".IsConfirmMessage())
        {
            await CommDataMethod<Daily>.DownloadToDbSetAsync(stockCode, startDate, endDate);
        }
    }

    private async Task LoadFinacialIndexOfSingleStockAsync()
    {
        if (IsWrongOfCheckFormatting("DownSingleStockOnPeriod"))
        {
            return;
        }

        if ("追加交易信息前，要删除原有时间段交易信息，是否同意?".IsConfirmMessage())
        {
            await CommDataMethod<FinancialIndex>.DownloadToDbSetAsync(stockCode, startDate, endDate);
        }
    }

    private static async Task LoadBasicNameOfStockAsync()
    {
        if ("更新基本信息前，要删除原有全部信息，是否同意?".IsConfirmMessage())
        {
            await CommDataMethod<StockBasic>.DownloadToDbSetAsync(null);
        }
    }

    private async Task LoadBakDailyOfSingleDailyAsync()
    {
        if (IsWrongOfCheckFormatting("DownAllStockOnSingleDaily"))
        {
            return;
        }

        if ("更新基本信息前，要删除原有全部信息，是否同意?".IsConfirmMessage())
        {
            await CommDataMethod<BakDaily>.DownloadToDbSetAsync(endDate);
        }
    }

    //批量导入单日或期间数据分标识:DownDataOnPeriod,DownDataOnDaily,DownDataOfFinance    
    private enum DownloadDataType
    {
        TradeOnPeriod,
        TradeOnDaily,
        FinanceOnPeriod
    }
}